<template>
    <div>
        <el-dialog :title="props.isAdd ? '添加菜单' : '编辑菜单'" v-model="props.isShow" center :before-close="beforeclose">
            <el-form :model="menuForm" ref="menuRef" :rules="rules">
                <el-form-item label="菜单名称" :label-width="formLabelWidth" prop="title">
                    <el-input v-model="menuForm.title" autocomplete="off" />
                </el-form-item>
                <el-form-item label="上级菜单" :label-width="formLabelWidth" prop="pid">
                    <el-select placeholder="Select" size="large" v-model="menuForm.pid">
                        <el-option :value="0" label="顶级菜单"></el-option>
                        <el-option v-for="item in store.getters['menu/getMenu']" :key="item.id" 
                        :label="item.title"   :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="菜单类型" :label-width="formLabelWidth">
                    <el-radio-group v-model="menuForm.type">
                        <el-radio :label="1" size="large">目录</el-radio>
                        <el-radio :label="2" size="large">菜单</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="菜单图标" :label-width="formLabelWidth">
                    <el-input v-model="menuForm.icon" autocomplete="off" />
                </el-form-item>
                <el-form-item label="菜单地址" :label-width="formLabelWidth">
                    <el-input v-model="menuForm.url" autocomplete="off" />
                </el-form-item>
                <el-form-item label="状态" :label-width="formLabelWidth">
                    <el-switch style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                        v-model="menuForm.status" :active-value="1" :inactive-value="2" />
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="cancel(menuRef)">取消</el-button>
                    <el-button type="primary" @click="confirm(menuRef)">确认</el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>
<script lang='ts' setup>
// 引入props方法
import { defineProps, ref, defineEmits, reactive, defineExpose } from 'vue'
import { ElMessage, FormInstance, FormRules } from 'element-plus'
// 引入添加接口
import { menuAdd, menuEdit, menuInfo } from '@/request/api'
// 引入vuex
import { useStore } from 'vuex'
let store = useStore()
const formLabelWidth = '120px'
// 定义props 必须用defineProps定义
let props = defineProps({
    isShow: Boolean,
    isAdd: Boolean
})
// 定义表单
let menuForm = ref({
    pid: 0,//上级分类编号 注意：顶级菜单-0number
    title: '',//菜单名称string
    icon: '',//图标string
    type: 1,//类型 1目录2菜单number
    url: '',//菜单地址string
    status: 1 //状态   1正常2禁用   number类型number
})
// 规则验证
const rules = reactive<FormRules>({
    title: [
        { required: true, message: '请输入菜单名称', trigger: 'blur' },
        { min: 2, max: 8, message: 'Length should be 2 to 8', trigger: 'blur' },
    ],
    pid: [
        {
            required: true,
            message: '请选择菜单',
            trigger: 'change',
        },
    ],
})
// 调用子传父
let emit = defineEmits(['cancel'])
// 取消事件
const cancel = (formEl: FormInstance | undefined) => {
    emit('cancel', false)
    // 清空数据
    menuForm.value = {
        pid: 0,//上级分类编号 注意：顶级菜单-0number
        title: '',//菜单名称string
        icon: '',//图标string
        type: 1,//类型 1目录2菜单number
        url: '',//菜单地址string
        status: 1 //状态   1正常2禁用   number类型number
    }
    if (!formEl) return
    // 清除规则验证
    formEl.resetFields()
}
const beforeclose = (done: () => {}) => {
    done()
    cancel(menuRef.value)
}
// 获取列表
const getMenu = () => {
    store.dispatch('menu/getMenuAction')
}
// 表单里面没有id  声明一个新的id
var newId=0
// 回显事件(获取一条数据)
const lookup = (id: number) => {
    menuInfo({ id }).then(res => {
        // console.log(res);
        menuForm.value = res.list as any
        newId=id
    })
}
let menuRef = ref<FormInstance>()
// 确认添加事件
const confirm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate(val => {
        if (val) {
            if (props.isAdd) {
                // 添加接口
                menuAdd(menuForm.value).then(res => {
                    console.log(res);
                    if (res.code == 200) {
                        ElMessage.success(res.msg)
                        cancel(formEl)
                        getMenu()
                    } else {
                        ElMessage.error(res.msg)
                    }

                })
            } else {
                // 编辑接口
                menuEdit({...menuForm.value,id:newId}).then(res => {
                    if (res.code == 200) {
                        ElMessage.success(res.msg)
                        cancel(formEl)
                        getMenu()
                    } else {
                        ElMessage.error(res.msg)
                    }
                })
            }


        }
    })
}
// 暴露lookup
defineExpose({
    lookup
})

</script>
<style scoped></style>